package com.party.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import com.party.domain.Collect;
import com.party.domain.Custom;
import com.party.domain.OrderDetail;
import com.party.domain.Orders;
import com.party.domain.Product;
import com.party.mapper.ProductMapper;
import com.party.mapper.ShoppingMapper;
import com.party.service.ShoppingService;
import com.party.tool.Time;


@Service
public class ShoppingServiceImpl implements ShoppingService
{
	
	private final Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);
	
	@Autowired
	private ShoppingMapper shoppingmapper;
	
	//增加商品数据
	public int addOrder(int uid)
	{
		//时间加上双随机数确保订单编号唯一
		String order_number=new Date().getTime()+Time.Ramdom()+Time.Ramdom();
		
		
		Orders orders=new Orders();
		orders.setUser_id(uid);
		orders.setOrder_number(order_number);
		orders.setOrder_time(new java.sql.Date(new java.util.Date().getTime()));
		orders.setIs_state(0);
		shoppingmapper.insterOrders(orders);
		return orders.getOid();
	}
	
	//增加商品详情数据
	public void addOrderDetail(int orders_id,int product_id,int product_number)
	{
		OrderDetail ord=new OrderDetail();
		ord.setOrders_id(orders_id);
		ord.setProduct_id(product_id);
		ord.setProduct_number(product_number);
		shoppingmapper.insterOrderDetail(ord);
	}

	//增加商品收藏
	public boolean addCollect(Collect collect)
	{
		
		List<Collect> colls=shoppingmapper.selectAllCollect(collect.getUser_id());
		for(Collect coll : colls)
		{
			if(coll.getProduct_id()==collect.getProduct_id())
			return false;
		}
		shoppingmapper.insterCollect(collect);
		return true;
	}
	
	//增加定制服务
	public void addCustom(Custom custom)
	{
		shoppingmapper.insterCustom(custom);
		
	}
	
	
	//按id删除定制服务
	public void deleteCustom(int cuid) throws Exception
	{
		Custom custom=shoppingmapper.selectIdCustom(cuid);
		if (custom == null) {
            throw new Exception("无id为 " + cuid + " 的定制服务");
        }
		shoppingmapper.deleteCustoms(cuid);
	}
	
	//分页查询所有定制服务
	public List<Custom> findAllCustom(int page,int size)
	{
		int start=size*(page-1);
		List<Custom> custom=shoppingmapper.selectAllCustom(start, size);
		
		if(custom==null){
			try {
				throw new Exception("查询所有定制服务为空");
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		return custom;
	}
	
	//id查询定制服务
	public Custom findIdCustom(int cuid) throws Exception
	{
		Custom custom=shoppingmapper.selectIdCustom(cuid);
		if(custom==null)
		{
				throw new Exception("无id为" + cuid + "的定制服务");
		}
		return custom;
	}
	
	//按手机号查询所有定制服务
	public List<Custom> findPhoneCustom(String phone,int page,int size)
	{
		int start=size*(page-1);
		List<Custom> custom=shoppingmapper.selectPhoneCustom(phone, start, size);
		if(custom==null){
			try {
				throw new Exception("该手机用户无定制服务");
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		return custom;
	}
	
	
	@Override
	public void deleteOrder(int oid) throws Exception 
	{
		Orders or=shoppingmapper.selectIdOrders(oid);
		if (or == null) {
            throw new Exception("无id为 " + oid + " 的订单");
        }
		
		shoppingmapper.deleteOrderDetail(oid);
		
	}

	
	//按id删除订单详情
	@Override
	public void deleteOrderDetail(int oid) throws Exception 
	{
		OrderDetail ord=shoppingmapper.selectIdOrderDetail(oid);
		if (ord == null) {
            throw new Exception("无id为 " + oid + " 的订单详情");
        }
		shoppingmapper.deleteOrderDetail(oid);	
	}
	
	
	public void deleteCollect(int coid) throws Exception
	{
		Collect collect=shoppingmapper.selectIdCollect(coid);
		if(collect==null){
			throw new Exception("无id为" + coid + "的收藏");
		}
		shoppingmapper.deleteCollect(coid);
	}
	
	
	public Collect findByCoidProdoct(int coid) throws Exception
	{
		
		Collect collect=shoppingmapper.findById(coid);
		if(collect==null){
			throw new Exception("无id为" + coid + "的收藏");
		}
		
		return collect;
	}
	
	public List<Collect> fidByUidProduct(int uid,int page,int size)
	{
		
		int start=size*(page-1);
		List<Collect> collect=shoppingmapper.findByIdCollect(uid,start, size);
		if(collect==null){
			try {
				throw new Exception("无用户id为" + uid + "的商品收藏");
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		return collect;
	}
	
	
	public List<Orders> findByUidOrders(int uid,int page,int size) throws Exception
	{
		int start=size*(page-1);
		List<Orders> orders=shoppingmapper.findByIdOrders(uid, start, size);
		if(orders==null)
		{
				throw new Exception("无用户id为" + uid + "的订单");

		}
		return orders;
		
	}
	
	
	//查询用户所有未支付订单
	public List<Orders> findByNoStateOrders(int uid,int page,int size) throws Exception
	{
		int start=size*(page-1);
		List<Orders> orders=shoppingmapper.findByNoStateOrder(uid, start, size);
		if(orders==null)
		{
				throw new Exception("无用户id为" + uid + "的未支付订单");

		}
		return orders;
	}
	
	public Orders fidByOidOrders(int oid) throws Exception
	{
		Orders orders=shoppingmapper.findByOId(oid);
		if(orders==null){
			throw new Exception("无id为" + oid + "的订单");
		}
		
		return orders;
	}
	
	
}
